package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.counter.domain.Thtype;
import com.ruoyi.counter.domain.Tools;
import com.ruoyi.counter.domain.ToolsCount;
import com.ruoyi.counter.mapper.ToolsMapper;
import com.ruoyi.counter.service.IThtypeService;
import com.ruoyi.counter.service.IToolsService;
import com.ruoyi.quartz.plusDay.plusDay;
import com.ruoyi.quartz.tsSMS.Sample;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.counter.service.impl.ToolsServiceImpl.plusDay;


/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */

@Component("ryTask")
public class RyTask {

    Logger log;
    @Autowired
    IToolsService iToolsService;

    @Autowired(required = false)
    ToolsMapper toolsMapper;

    @Autowired
    IThtypeService iThtypeService;
    @Autowired
    ISysUserService iSysUserService;


    /**
     * 16      * 指定日期加上天数后的日期
     * 17      * @param num 为增加的天数
     * 18      * @param newDate 创建时间
     * 19      * @return
     * 20      * @throws ParseException
     * 21
     */

    /**
     * 定时任务，每天早上8点执行，更新工器具的定检状态
     */

    public void ryParamsStatisticsTools(String params) throws ParseException {

        List<Tools> toolsList = new ArrayList<>();
        Tools toolstmp = new Tools();
        toolsList = iToolsService.selectToolsAllList(toolstmp);
        System.out.println("获取所有工器具列表：======" + toolsList.size());
        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(d);
        System.out.println("格式化后今天的日期为：" + today);
        plusDay(-30, today);
        String lateDate = plusDay(-30, today);
        System.out.println("工器具的数量是 " + toolsList.size());
        for (int i = 0; i < toolsList.size(); i++) {
            Tools tools = new Tools();
            tools = toolsList.get(i);
            if (tools.getToosstatus().equals("0")) {
                if (tools.getIsouttm().compareTo(today) == 0) {
                    tools.setToosstatus("5");

                    log.isDebugEnabled();
                    iToolsService.updateTools(tools);
                } else if (tools.getIsouttm().compareTo(today) > 0) {
                    tools.setToosstatus("4");
                    iToolsService.updateTools(tools);
                } else {
                    //notodo
                }

            }

        }


    }


    /**
     * 定时任务，每天早上9点执行，检查所有机构下对应的工器具有没有临定检的，有就发送给设置为发送短信的哦人员
     * 默认临检一个月的周期
     */
    public void ryParamsCheckDjInfo(String params) throws ParseException {


        //查询需要发短信的内容  @tongshuai 处理
        // 通过工器具找到 机构， 找到发短信人员的手机号码的信息，找出对应的手机号码，汇总临检预警的个数（到期或即将到期的个数）信息发短信
        //刷新定检的日期
        ToolsCount toolsCountDJ = new ToolsCount();
        List<ToolsCount> toolsDJCountList = new ArrayList<>(toolsMapper.selectToolsDJCount(toolsCountDJ));

        System.out.println("toolsDJCountList============" + toolsDJCountList);
        ToolsCount toolsCountLJ = new ToolsCount();
        List<ToolsCount> toolsLjCountList = toolsMapper.selectToolsljCount(toolsCountLJ);
        System.out.println(toolsDJCountList + "==============================" + toolsLjCountList);
        int djNum = 0;
        int ljNum = 0;
        /**
         * 到检检测
         */
        for (int j = 0; j < toolsDJCountList.size(); j++) {
            djNum = ljNum = 0;
            djNum = toolsDJCountList.get(j).getTipNum();

            for (int k = 0; k < toolsLjCountList.size(); k++) {
                if (toolsDJCountList.get(j).getDeptId().equals(toolsLjCountList.get(k).getDeptId())) {
                    ljNum = toolsLjCountList.get(k).getTipNum();
                    System.out.println("临检数量是：" + ljNum);
                    break;
                }
            }
            if (djNum > 0 || ljNum > 0) {
                //查找用户信息 简单处理把用户都拉出来，程序比较
                SysUser sysUser = new SysUser();
                sysUser.setDeptId(Long.valueOf(toolsDJCountList.get(j).getDeptId()));

                System.out.println(sysUser);

                List<SysUser> sysUserListUser = new ArrayList<>();
                //数据隔离只能查本地的
                /**
                 * 重写service方法，直接跳过条件查询用户
                 */

                sysUserListUser = iSysUserService.selectUserListlist(sysUser);

                System.out.println("获取到的所有用户为：============" + sysUserListUser.size());
                for (int m = 0; m < sysUserListUser.size(); m++) {
                    System.out.println(sysUserListUser.get(m).getPhonenumber());
                    if (sysUserListUser.get(m).getYxno().equals("1") && String.valueOf(sysUserListUser.get(m).getDeptId()).equals(toolsDJCountList.get(j).getDeptId())) {

//                            //发送短信 有 djNum 工器具过了定检时间， 有 ljNum 的工器具已到临检日期  仝帅组织短信发送 @tongshuai,异常就break，不需要return
                        System.out.println(sysUserListUser.get(m).getPhonenumber());
                        /**
                         * @param 短信发送功能，已调通，勿删
                         */
                            String powerName = sysUserListUser.get(m).getUserName();
                            System.out.println(powerName);
                            int num = ljNum+djNum;
                            System.out.println(num);
                            String phoneNum = sysUserListUser.get(m).getPhonenumber();
                            System.out.println(phoneNum);
                            String content = "临检";
                        Sample sample = new Sample();
                        sample.sendMSG(powerName, num, phoneNum, content);

                        break;

                    }

                }
            }
        }//for (int j = 0; j < toolsDJCountList.size(); j++)

    }


    /**
     * 通过管理平台手动执行 定检日期重算功能 （手动点击），参数预留，目前是全部重新算
     */
    public void ryParamsresetToolsdjInfo(String params) throws ParseException {
        //查询需要发短信的内容  @tongshuai 处理
        // 获取所有的工器具，重算定见日期 不考虑报废的，报损的，过定检时间的
        List<Tools> toolsList = new ArrayList<>();
        Tools toolstmp = new Tools();
        toolsList = iToolsService.selectToolsAllList(toolstmp);
        Thtype thtype = new Thtype();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
//        String datePreStr = plusDay(-30, dateNowStr);
        List<Thtype> thtypeList = new ArrayList<>();
        thtypeList = iThtypeService.selectThtypeList(thtype);
        System.out.println("获取到的工器具类型是:=============" + thtypeList.size());
        System.out.println("工器具的数量是 " + toolsList.size());
        for (int i = 0; i < toolsList.size(); i++) {
            Tools tools = new Tools();
            tools = toolsList.get(i);
            if (tools.getToosstatus().equals("0")) {
                //找到定检的周期
                for (int j = 0; j < thtypeList.size(); j++) {
                    if (thtypeList.get(j).getTm() != null && thtypeList.get(j).getTm() > 0) {
                        tools.setIsouttm(plusDay(thtypeList.get(j).getTm().intValue() * 30, dateNowStr));
                        iToolsService.updateTools(tools);
                    }
                }
            }
        }
    }

//    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
//        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
//    }

    public void ryParams(String params) {

        System.out.println("执行有参方法：" + params);
    }

    public void ryParamsTestTest(String params) {
        /**
         * 定时更新当天工器具领用归还数据----1
         */
//        CounterStatisticalList counterStatisticalList = new CounterStatisticalList();
//        List<CounterStatisticalList> list = new ArrayList<>();
//        list = iCounterStatisticalListService.selectCounterStatisticalListChect(counterStatisticalList);
//        String a = list.toString();
//        System.out.println(a);
//        for (CounterStatisticalList job : list) {
//            iCounterStatisticalListService.insertCounterStatisticalList(job);
//
//            // job对象是每一条数据，isInsert字段填入1
//            //todo 在你的原表里面 把这些更新过的数据 isInsert字段 插入1
//            // 下次插入的时候，加一个判断条件 isInsert字段是否为1，如果为1，那么就是老数据，就不要插入了
//        }

//        /**
//         * 定时更新当天工器具领用归还数据----2
//         */

    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }
}
